package org.zxstudio.encrypt;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.zxstudio.encrypt.MediaBaseAdapter.onItemSelectedListener;
import org.zxstudio.encrypt.utils.MediaComparator;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AbsListView;
import android.widget.GridView;
import android.widget.ImageView;

public class MultiMediaActivity extends Activity {

	private static final String TAG = "MultiMediaActivity";
	private static final boolean DEBUG = true;

	private GridView mGridView;
	private View  mOptionView;
	private MediaBaseAdapter mMediaAdapter;

	private List<MediaBean> mFileList = new ArrayList<MediaBean>();

	private ExplorerMediaTask mSyncTask;
	
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.multi_media);

		initRes();
	}

	private void initRes() {
		mFileList.clear();
		mGridView = (GridView) findViewById(R.id.gridview);
		mGridView.setOnScrollListener(new AbsListView.OnScrollListener() {
			
			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				if (scrollState == OnScrollListener.SCROLL_STATE_IDLE) {
					if (DEBUG) {
						Log.v(TAG,"when grid view idle  refresh icon");
					}
				/*	if (mMediaAdapter != null) {
						mMediaAdapter.notifyDataSetChanged();
					}*/
				}
			}
			
			@Override
			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {
				
			}
		});
		mOptionView = findViewById(R.id.option_view);
		mMediaAdapter = new MediaBaseAdapter(this, mFileList);
		mMediaAdapter.registerItemSelectListener(onItemSelectedListener);
		mGridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View view,
					int location, long id) {
			
				mMediaAdapter.updateSelectItem(view,location);
			}

		});
		mGridView.setAdapter(mMediaAdapter);

		mSyncTask = new ExplorerMediaTask();
		mSyncTask.execute(0);
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
	}

	private void queryVideo() {
		ContentResolver contentResolver = getContentResolver();
		String[] projection = new String[] { MediaStore.Video.Media._ID,
				MediaStore.Video.Media.TITLE, MediaStore.Video.Media.DATA };
		Cursor cursor = contentResolver.query(
				MediaStore.Video.Media.EXTERNAL_CONTENT_URI, projection, null,
				null, MediaStore.Video.Media.DEFAULT_SORT_ORDER);
		MediaBean domain = null;
		while (cursor.moveToNext()) {
			domain = new MediaBean();
			long id = cursor.getLong(0);
			String title = cursor.getString(1);
			String data = cursor.getString(2);
			domain.id = id;
			if (TextUtils.isEmpty(title)) {
				File file = new File(data);
				if (file.exists()) {
					title = file.getName();
				} else {
					title = this.getResources().getString(R.string.unknown);
				}
			}
			domain.title = title;
			domain.data = data;
			domain.type = MediaBean.VIDEO;
			mFileList.add(domain);

		}
		cursor.close();
	}

	private void queryAudio() {
		ContentResolver contentResolver = getContentResolver();
		String[] projection = new String[] { MediaStore.Audio.Media._ID,
				MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.DATA };
		Cursor cursor = contentResolver.query(
				MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, projection, null,
				null, MediaStore.Video.Media.DEFAULT_SORT_ORDER);

		MediaBean domain = null;
		while (cursor.moveToNext()) {
			domain = new MediaBean();
			long id = cursor.getLong(0);
			String title = cursor.getString(1);
			String data = cursor.getString(2);
			domain.id = id;
			if (TextUtils.isEmpty(title)) {
				File file = new File(data);
				if (file.exists()) {
					title = file.getName();
				} else {
					title = this.getResources().getString(R.string.unknown);
				}
			}
			domain.title = title;
			domain.data = data;
			domain.type = MediaBean.AUDIO;
			mFileList.add(domain);

		}
		cursor.close();
	}

	private void queryImage() {
		ContentResolver contentResolver = getContentResolver();
		String[] projection = new String[] { MediaStore.Images.Media._ID,
				MediaStore.Images.Media.TITLE, MediaStore.Images.Media.DATA };
		Cursor cursor = contentResolver.query(
				MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null,
				null, MediaStore.Images.Media.DEFAULT_SORT_ORDER);
		MediaBean domain = null;
		while (cursor.moveToNext()) {
			domain = new MediaBean();
			long id = cursor.getLong(0);
			String title = cursor.getString(1);
			String data = cursor.getString(2);
			domain.id = id;
			if (TextUtils.isEmpty(title)) {
				File file = new File(data);
				if (file.exists()) {
					title = file.getName();
				} else {
					title = this.getResources().getString(R.string.unknown);
				}
			}
			domain.title = title;
			domain.data = data;
			domain.type = MediaBean.IMAGE;
			mFileList.add(domain);

		}
		cursor.close();
	}

	private class ExplorerMediaTask extends AsyncTask<Integer, Integer, Void> {

		private ProgressDialog dialog;

		@Override
		protected void onCancelled() {
			super.onCancelled();
		}

		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
			dialog.dismiss();
			mMediaAdapter.notifyDataSetChanged();
		}

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			dialog = ProgressDialog.show(
					MultiMediaActivity.this,
					"",
					MultiMediaActivity.this.getResources().getString(
							R.string.explorer_sd_file));
		}

		@Override
		protected void onProgressUpdate(Integer... values) {
			super.onProgressUpdate(values);
		}

		@Override
		protected Void doInBackground(Integer... params) {
			queryMediaFromDB();
			return null;
		}

	}

	private void queryMediaFromDB() {
		queryVideo();
		queryAudio();
		queryImage();
		int size = mFileList.size();
		MediaBean[] domainArray = (MediaBean[])mFileList.toArray(new MediaBean[size]);
		Arrays.sort(domainArray,new MediaComparator());
		mFileList.clear();
		for (MediaBean domain: domainArray) {
			mFileList.add(domain);
		}
	}
	
	private MediaBaseAdapter.onItemSelectedListener  onItemSelectedListener = new MediaBaseAdapter.onItemSelectedListener() {
		
		@Override
		public void noItemSelected() {
			if (mOptionView.getVisibility() != View.GONE) {
				Animation ani = AnimationUtils.loadAnimation(
						MultiMediaActivity.this, R.anim.btn_translate_g);
				mOptionView.startAnimation(ani);
				mOptionView.setVisibility(View.GONE);
			}
		}
		
		@Override
		public void hasItemSelected() {
			if (DEBUG) {
				Log.v(TAG,"hasItemSelected");
			}
			if (mOptionView.getVisibility() == View.GONE) {
				Animation ani = AnimationUtils.loadAnimation(
						MultiMediaActivity.this, R.anim.btn_translate_v);
				mOptionView.startAnimation(ani);
				mOptionView.setVisibility(View.VISIBLE);
			}
		}
	};

}
